package com.zhku.zeng.util;

import freemarker.template.*;

import java.io.*;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class FreeMarkUtil {
    private Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);

    private Template template;
    public FreeMarkUtil(String projectUrl) throws IOException {
        cfg.setDirectoryForTemplateLoading(new File(projectUrl));
        cfg.setObjectWrapper(new DefaultObjectWrapper(Configuration.VERSION_2_3_23));
    }

    public void parse(String templateName,String fileName,Map<String,Object> data) throws IOException, TemplateException {
        template = cfg.getTemplate(templateName);
        FileWriter fileWriter = new FileWriter(fileName);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        template.process(data, bufferedWriter);

    }

}
